"use strict";
cc._RF.push(module, 'f079894QKBJKIsQcMmI9prv', 'roomlist_pop');
// scripts/chatRoom/roomlist_pop.js

'use strict';

// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/**
 * 聊天室
 * 房间列表界面
 */
cc.Class({
    extends: cc.Component,

    properties: {
        ndRoomItemPre: cc.Prefab, //彩种房间列表预制
        ndRoomList: cc.Node,
        betRoom: cc.Prefab, //对应彩种投注房间界面
        PlayPrefab: cc.Prefab, //玩法说明界面预制
        _lotteryID: null, //彩种编号(ID)
        _serRoomList: null, //服务器发来的房间信息
        _roomlist: null //聊天服务器发来的房间信息

    },

    // LIFE-CYCLE CALLBACKS:
    onLoad: function onLoad() {
        this._roomlist = null;
        this._serRoomList = null;
        this.resultStr = {
            '40010': '', //幸运28图片
            '40020': '', //加拿大28图片
            '301': '', //老时时彩图片 
            '202': '' //老11选5图片 
        };
        window.Notification.on('rroomlist_push', function (recv) {
            cc.log("chat rroomlist_push:" + recv);
            this.unschedule(this.endUpdate);
            if (recv.code == 1) {
                this._roomlist = recv.data;
                this.initRoomList(this._serRoomList, this._roomlist);
            } else {
                this.initRoomList(this._serRoomList, null);
            }
        }.bind(this));
    },

    start: function start() {
        var recv = function (ret) {
            ComponentsUtils.unblock();
            if (ret.Code !== 0) {
                cc.error(ret.Msg);
            } else {
                if (ret.Code == 49 || ret.Code == 997) {
                    //数据为空(null)或系统繁忙
                    cc.error('没有数据或系统繁忙');
                    return;
                }
                this._serRoomList = ret.Data;

                // //长连获取房间列表
                //   cc.log("send chat roomlist:"+this._lotteryID);
                CL.NET.send("roomlist", this._lotteryID);
                //长连3s没返回就直接显示
                this.schedule(this.endUpdate, 3);
            }
        }.bind(this);
        var data = {
            Token: User.getLoginToken(),
            LotteryCode: this._lotteryID
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETROOMLIST, data, recv.bind(this), "POST");
        ComponentsUtils.block();
    },

    init: function init(data) {
        this._lotteryID = data;
    },

    endUpdate: function endUpdate() {
        //cc.log('chat not recv roomlist');
        this.unschedule(this.endUpdate);
        this.initRoomList(this._serRoomList, null);
    },

    //初始并显示房间列表
    initRoomList: function initRoomList(data, data1) {
        if (this.ndRoomList && this.ndRoomList.children.length <= 0) {
            for (var i = 0; i < data.length; ++i) {
                var itemRoom = cc.instantiate(this.ndRoomItemPre);

                if (data1 != null) {
                    var data1Obj = Utils.findObjByArry(data1, 'roomid', data[i].RoomID.toString());
                    if (data1Obj != null && data1Obj.roomonline != null) {
                        data[i].RoomNums = data1Obj.roomonline;
                        cc.log('chat roomlist 聊天室在线人数：' + data[i].RoomNums);
                    } else {
                        data[i].RoomNums = Utils.getRandomNum(100, data[i].RoomNums);
                        cc.log("chat roomlist 聊天室找不到房间：" + data[i].RoomID + '随机人数:' + data[i].RoomNums);
                    }
                } else {
                    data[i].RoomNums = Utils.getRandomNum(100, data[i].RoomNums);
                    cc.log("chat roomlist 聊天室房间消息为null随机人数:" + data[i].RoomNums);
                }

                itemRoom.getComponent('roomlist_btn_item').init({
                    roomNums: data[i].RoomNums,
                    roomName: data[i].RoomName,
                    roomCode: data[i].RoomCode
                });
                this.ndRoomList.addChild(itemRoom);

                var checkEventHandler = new cc.Component.EventHandler();
                checkEventHandler.target = this.node;
                checkEventHandler.component = "roomlist_pop";
                checkEventHandler.handler = "onRoomBtn";
                checkEventHandler.customEventData = {
                    lottery: this._lotteryID,
                    chatRoomID: data[i].RoomID
                };
                itemRoom.getComponent(cc.Button).clickEvents.push(checkEventHandler);
            }
        }
    },

    //进入对应彩种投注房间
    onRoomBtn: function onRoomBtn(event, customEventData) {
        //   cc.log('roomlist_pop 请求进入房间,id:',customEventData.chatRoomID);
        var canvas = cc.find('Canvas');
        var betRoom = cc.instantiate(this.betRoom);
        betRoom.getComponent('room_chat_pop').init(customEventData);
        canvas.addChild(betRoom);
    },

    //玩法说明
    onPlayingRule: function onPlayingRule() {
        var canvas = cc.find("Canvas");
        var res = '/text/rule' + this._lotteryID;
        var ii = this.resultStr[this._lotteryID];
        if (this.resultStr[this._lotteryID] != undefined) {
            if (this.resultStr[this._lotteryID] != "") {
                var PlayPrefab = cc.instantiate(this.PlayPrefab);
                var str = LotteryUtils.getLotteryTypeDecByLotteryId(this._lotteryID) + '玩法说明';
                PlayPrefab.getComponent(PlayPrefab.name).init(str, this.resultStr[this._lotteryID]);
                canvas.addChild(PlayPrefab);
            } else {
                cc.loader.loadRes(res, function (error, result) {
                    if (error) {
                        return;
                    }
                    this.resultStr[this._lotteryID] = result;
                    var PlayPrefab = cc.instantiate(this.PlayPrefab);
                    var str = LotteryUtils.getLotteryTypeDecByLotteryId(this._lotteryID) + '玩法说明';
                    PlayPrefab.getComponent(PlayPrefab.name).init(str, result);
                    canvas.addChild(PlayPrefab);
                }.bind(this));
            }
        }
    },

    //关闭界面
    onClose: function onClose() {
        this.unschedule(this.endUpdate);
        window.Notification.offType("rroomlist_push");
        this.node.getComponent("Page").backAndRemove();
    }

    // update (dt) {},
});

cc._RF.pop();